<!DOCTYPE html>
<html>
<title>隐患地图</title>
<head>
<title>隐患添加</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport"
	content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="format-detection" content="telephone=no" />
<meta charset="utf-8" />
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script src="json2.js"></script>
<style type="text/css">
body, html, #allmap {
	width: 100%;
	height: 100%;
	margin: 0;
	font-family: "微软雅黑";
}
</style>
<script type="text/javascript"
	src="http://api.map.baidu.com/api?v=2.0&ak=cR98TwX2FnKTpANIOYhMYMot9a4ioxjc"></script>
<script type="text/javascript"
	src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script>
</head>
<body>
	<div id="allmap"></div>
</body>
</html>
<script type="text/javascript">

	//百度地图API功能
	var top_left_control = new BMap.ScaleControl({
		anchor : BMAP_ANCHOR_TOP_LEFT
	});// 左上角，添加比例尺
	var top_left_navigation = new BMap.NavigationControl(); //左上角，添加默认缩放平移控件

	// var map = new BMap.Map("allmap");
	var map = new BMap.Map("allmap", {
		enableMapClick : false
	});//构造底图时，关闭底图可点功能

	map.addControl(top_left_control);
	map.addControl(top_left_navigation);

	var xhm = new ActiveXObject("Microsoft.XMLHttp");
	xhm.open("GET", "../baiduMap/getPosition.do", false);
	xhm.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhm.send();
	var json = JSON.parse(xhm.responseText);
	var point = new BMap.Point(json.lng, json.lat);
	map.centerAndZoom(point, 13);

	var xhm = new ActiveXObject("Microsoft.XMLHttp");
	xhm.open("GET", "../baiduMap/location.do", false);
	xhm.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhm.send();
	var ticket = JSON.parse(xhm.responseText);
	var point = new BMap.Point(ticket.content.point.x, ticket.content.point.y);
	var str = "我的位置";
	var opts = {
		position : point
	// 指定文本标注所在的地理位置
	}
	var label = new BMap.Label(str, opts); // 创建文本标注对象
	label.setStyle({
		fontSize : "12px",
		height : "20px",
		lineHeight : "20px",
		fontFamily : "微软雅黑"
	});
	map.addOverlay(label);

	map.enableScrollWheelZoom(true);
	var geoc = new BMap.Geocoder();

	//获取标注位置信息    
	function getAttr() {
		var p = marker.getPosition(); //获取marker的位置
		alert("marker的位置是" + p.lng + "," + p.lat);
	}

	var xhm = new ActiveXObject("Microsoft.XMLHttp");
	xhm.open("GET", "../baiduMap/getByPoint.do?limit=1000&offset=0", false);
	xhm.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhm.send();
	var ticket = JSON.parse(xhm.responseText);
	addDeck(ticket);

	function addDeck(ticket) {

		// console.log(ticket);
		var json_data = new Array();

		for (var i = 0; i < ticket.length; i++) {
			json_data[i] = [ ticket[i].lng, ticket[i].lat, ticket[i].name,
					ticket[i].hidden_level ];
		}

		var pointArray = new Array();
		for (var i = 0; i < json_data.length; i++) {
			var point = new BMap.Point(json_data[i][0], json_data[i][1]);
			var myIcon = new BMap.Icon("../img/hiddenPoint2.png",
					new BMap.Size(10, 30));
			var marker = new BMap.Marker(point, {
				icon : myIcon
			}); // 创建点
			if (json_data[i][3] == "重大隐患") {
				var myIcon = new BMap.Icon("../img/hiddenPoint0.png",
						new BMap.Size(10, 30));
				marker = new BMap.Marker(point, {
					icon : myIcon
				}); // 创建点
			} else if (json_data[i][3] == "一级隐患") {
				var myIcon = new BMap.Icon("../img/hiddenPoint1.png",
						new BMap.Size(10, 30));
				marker = new BMap.Marker(point, {
					icon : myIcon
				}); // 创建点
			} else if (json_data[i][3] == "二级隐患") {
				var myIcon = new BMap.Icon("../img/hiddenPoint2.png",
						new BMap.Size(10, 30));
				marker = new BMap.Marker(point, {
					icon : myIcon
				}); // 创建点
			}
			map.addOverlay(marker); //增加点
			marker.addEventListener("click", attribute);
		}
		//让所有点在视野范围内
		map.setViewport(pointArray);

	}

	//获取覆盖物位置
	function attribute(e) {
		var p = e.target;
		//alert("marker的位置是" + p.getPosition().lng + "," + p.getPosition().lat);   
		var xhm = new ActiveXObject("Microsoft.XMLHttp");
		xhm.open("GET", "../baiduMap/getGUIDByPosition.do?lng="
				+ p.getPosition().lng + "&lat=" + p.getPosition().lat, false);
		xhm.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded");
		xhm.send();
		// alert(xhm.responseText);
		var data=JSON.parse(xhm.responseText);

		xhm.open("GET", "/shambles/mobile/asset/getAssetByHidden.do?limit=100&offset=0"+
				"&hiddenGuid="+data.guid, false);
		xhm.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded");
		xhm.send();
		
		var hidden=data.hidden;
		
		var data2=JSON.parse(xhm.responseText); 
		
		var rows=data2.rows;
		
		var assets="";
		
		for(var i=0;i<rows.length;i++){
		    assets=assets+rows[i].address+",";
		}
		
		assets=assets.substring(0,assets.length-1);
		
		var content="<h4 style='margin:0 0 5px 0;padding:0.2em 0'> 隐患名称 : "+hidden.name+"</h4>"+
		"<p>"+
   	 	" 管理分区 : "+hidden.manageRegion+" </br>"+
    	" 隐患等级 : "+hidden.hidden_level+" </br>"+
    	" 隐患状态 : "+hidden.state+" </br>"+
    	" 隐患详情 : "+hidden.detail+" </br>"+
    	" 关联资产 : "+assets+" </br>"+
    	" 录入人员 : "+hidden.user_name+" </br>"+
    	" 录入时间 :"+actionTime(hidden.date)+" </br>"+
    	"</p>"+"</div>";
    	
		var infoWindow = new BMap.InfoWindow(content);  // 创建信息窗口对象
        map.openInfoWindow(infoWindow,new BMap.Point(e.point.lng, e.point.lat));

	}

	
	var opts = {
			width : 250, // 信息窗口宽度
			height : 120, // 信息窗口高度
			title : "资产详情", // 信息窗口标题
			enableMessage : true
		//设置允许信息窗发送短息
		};

	function openInfo(content, e) {
			var p = e.target;
			var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
			var infoWindow = new BMap.InfoWindow(content, opts); // 创建信息窗口对象 
			map.openInfoWindow(infoWindow, point); //开启信息窗口
		}
	
	function getByteLen(val) { //传入一个字符串
		var len = 0;
		for (var i = 0; i < val.length; i++) {
			if (val[i].match(/[^\x00-\xff]/ig) != null) //全角 
				len += 2; //如果是全角，占用两个字节  如果mysql中某字段是text, 如果设置编码为utf-8,那么一个中文是占3个字节, gbk是两个字节
			else
				len += 1; //半角占用一个字节
		}
		return len;
	}
	
	function actionTime(value){
  	    var date = new Date(value); 
  	    Y = date.getFullYear() + '年';
  	    M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '月';
  	    D = date.getDate() + '日 ';
  	    h = date.getHours() + ':';
  	    m = date.getMinutes() + ':';
  	    s = date.getSeconds(); 
  		return Y+M+D+h+m+s;
  	}
	
</script>
